package com.shadowtorcher.stdisk.logic;

import com.shadowtorcher.soulknife.SoulKnifeDB;
import com.shadowtorcher.soulknife.exception.BeanConfigErrorException;
import com.shadowtorcher.stdisk.bean.LogicFileBean;

import java.sql.SQLException;
import java.util.Date;

public class FileMover {
    private LogicFileBean logicFile;

    public FileMover(LogicFileBean logicFile) {
        this.logicFile = logicFile;
    }

    public static FileMover getFromUUID(String uuid) throws SQLException {
        try {
            LogicFileBean logicFileBean = SoulKnifeDB.selectWhere("LogicFileID = ?", uuid).setClass(LogicFileBean.class).get(0);
            return new FileMover(logicFileBean);
        } catch (BeanConfigErrorException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void move(String path, String filename) throws SQLException {
        logicFile.setLogicPath(path);
        logicFile.setLogicFileName(filename);
        logicFile.setModifiDate(new Date());
        try {
            SoulKnifeDB.updateWhere(logicFile, "LogicFileID = ?", logicFile.getLogicFileID()).setClass(LogicFileBean.class);
        } catch (BeanConfigErrorException e) {
            e.printStackTrace();
        }
    }

    public void move(String path) throws SQLException {
        logicFile.setLogicPath(path);
        logicFile.setModifiDate(new Date());
        try {
            SoulKnifeDB.updateWhere(logicFile, "LogicFileID = ?", logicFile.getLogicFileID()).setClass(LogicFileBean.class);
        } catch (BeanConfigErrorException e) {
            e.printStackTrace();
        }
    }

    public void rename(String filename) throws SQLException {
        logicFile.setLogicFileName(filename);
        logicFile.setModifiDate(new Date());
        try {
            SoulKnifeDB.updateWhere(logicFile, "LogicFileID = ?", logicFile.getLogicFileID()).setClass(LogicFileBean.class);
        } catch (BeanConfigErrorException e) {
            e.printStackTrace();
        }
    }


    public String getPath() {
        return logicFile.getLogicPath();
    }

    public String getFileName() {
        return logicFile.getLogicFileName();
    }
}
